import axios from 'axios';

import type { AxiosInstance } from 'axios';
import type { HYRequestInterceptors, XJHRequestConfig } from './type';
import { ElLoading } from 'element-plus';
import configs from './config';
// import { ILoadingInstance } from 'element-plus/lib/el-loading/src/loading.type';
class XJHRequest {
  // 创建axios实例的基本配置
  instance: AxiosInstance;
  interceptors?: HYRequestInterceptors;
  showLoading: boolean;
  loading?: any;
  constructor(config: XJHRequestConfig) {
    this.instance = axios.create(config);
    //  判断是(??) 是一个undefined 就会去后面
    this.showLoading = config.showLoading ?? configs.DEFAULT_LOADING;
    this.interceptors = config.interceptors;

    // 所有实例的拦截器
    this.instance.interceptors.request.use(
      (config) => {
        // console.log('all request', config);
        if (this.showLoading) {
          this.loading = ElLoading.service({
            lock: true,
            text: '正在请求数据。。。',
            fullscreen: true,
            spinner: 'el-AddLocation',
            background: 'rgba(0,0,0,0.5)'
          });
        }
        // 进行错误处理
        return config;
      },
      (err) => {
        return err;
      }
    );
    this.instance.interceptors.response.use(
      (res) => {
        // console.log('all response', config);
        setTimeout(() => {
          this.loading?.close();
        }, 1000);
        return res.data;
      },
      (err) => {
        return err;
      }
    );

    // 实例拦截器,就是因为我是保存在一个地方这样罢了。。
    this.instance.interceptors.request.use(
      this.interceptors?.requestInterceptor,
      this.interceptors?.requestInterceptorCatch
    );
    this.instance.interceptors.response.use(
      this.interceptors?.responseInterceptor,
      this.interceptors?.responseInterceptorCatch
    );
  }

  // 单个请求进行处理
  request<T = any>(config: XJHRequestConfig<T>): Promise<T> {
    return new Promise((resolve, reject) => {
      if (config.interceptors?.requestInterceptor) {
        // console.log('this requestInterceptor', config);
        config = config.interceptors.requestInterceptor(config);
      }
      if (config.showLoading === false) {
        this.showLoading = config.showLoading;
      }
      // 进行路柯里化操作
      this.instance
        .request<any, T>(config)
        .then((res) => {
          if (config.interceptors?.responseInterceptor) {
            // console.log('this responseInterceptor', config);
            res = config.interceptors.responseInterceptor(res);
          }
          this.showLoading = configs.DEFAULT_LOADING;
          // console.log(res);
          resolve(res);
        })
        .catch((err) => {
          this.showLoading = configs.DEFAULT_LOADING;
          reject(err);
          return err;
        });
    });
  }

  get<T = any>(config: XJHRequestConfig<T>): Promise<T> {
    return this.request<T>({ ...config, method: 'GET' });
  }

  post<T = any>(config: XJHRequestConfig<T>): Promise<T> {
    return this.request<T>({ ...config, method: 'POST' });
  }

  delete<T = any>(config: XJHRequestConfig<T>): Promise<T> {
    return this.request<T>({ ...config, method: 'DELETE' });
  }

  patch<T = any>(config: XJHRequestConfig<T>): Promise<T> {
    return this.request<T>({ ...config, method: 'PATCH' });
  }
}

export default XJHRequest;
